﻿using System;
using System.Collections.Generic;
using System.Text;
using TomShane.Neoforce.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Warlock.GameStuff;

namespace Warlock.Menu
{
    class MainMenu : IGameScreen
    {
        public Game1 MainGame { get; set; }

        ContentManager content;
        GraphicsDeviceManager graphics;
        Manager manager;
        Window mainWindow;
        ImageBox BackGroundImageBox;
        MenuItemButton StartGameButton;
        MenuItemButton OptionsButton;
        MenuItemButton ExitButton;

        Texture2D backGroundTexture;

        OptionsWindow optionsWindow;

        public MainMenu(Game1 game)
        {
            MainGame = game;
            manager = Game1.Manager;
        }

        void StartButton_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            //Alle Fenster wegmachen
            if (optionsWindow != null)
                optionsWindow.Close();

            //SingleplayerStartScreen startScreen = new SingleplayerStartScreen();

            SingleOrMultiplayerMenu spOrMpMenu = new SingleOrMultiplayerMenu();

            MainGame.SwitchGamescreen(spOrMpMenu);
        }
        void OptionsButton_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            optionsWindow = new OptionsWindow(manager, graphics, content);
            optionsWindow.Init();
            optionsWindow.ResolutionChanged += new ResolutionChangedEventHandler(optionsWindow_ResolutionChanged);
            manager.Add(optionsWindow);
        }

        void optionsWindow_ResolutionChanged(ResolutionChangedEventArgs e)
        {
            UpdateCtrls();
        }
        void ExitButton_Click(object sender, TomShane.Neoforce.Controls.EventArgs e)
        {
            MainGame.Exit();
        }

        public void Initialize(GraphicsDeviceManager graphics, ContentManager content)
        {

            MainGame.IsMouseVisible = true;
            this.content = content;
            this.graphics = graphics;

            manager.Initialize();

            mainWindow = new Window(manager);
            mainWindow.Init();
            mainWindow.Width = graphics.PreferredBackBufferWidth;
            mainWindow.Height = graphics.PreferredBackBufferHeight;
            mainWindow.Visible = true;
            mainWindow.Left = 0;
            mainWindow.Top = 0;
            mainWindow.CloseButtonVisible = false;
            mainWindow.Resizable = false;
            mainWindow.Movable = false;
            mainWindow.Color = Color.Black;
            mainWindow.Text = "";
            mainWindow.IconVisible = false;
            mainWindow.AutoScroll = false;
            mainWindow.MaximumWidth = 5000;
            mainWindow.MaximumHeight = 5000;

            backGroundTexture = content.Load<Texture2D>("Resources\\Menu\\Menue");
            BackGroundImageBox = new ImageBox(manager);
            BackGroundImageBox.Init();
            BackGroundImageBox.Parent = mainWindow;
            BackGroundImageBox.Image = backGroundTexture;
            BackGroundImageBox.Width = mainWindow.ClientWidth;
            BackGroundImageBox.Height = mainWindow.ClientHeight;
            BackGroundImageBox.SizeMode = SizeMode.Stretched;
            BackGroundImageBox.Anchor = Anchors.All;

            StartGameButton = new MenuItemButton();
            StartGameButton.Init(manager, content);
            StartGameButton.Position = new Vector2(graphics.PreferredBackBufferWidth / 2 - StartGameButton.Button.Width / 2, 25);
            StartGameButton.Button.Text = "Neues Spiel";
            StartGameButton.Button.Parent = mainWindow;
            StartGameButton.Button.Click += new TomShane.Neoforce.Controls.EventHandler(StartButton_Click);

            OptionsButton = new MenuItemButton();
            OptionsButton.Init(manager, content);
            OptionsButton.Position = new Vector2(graphics.PreferredBackBufferWidth / 2 - OptionsButton.Button.Width / 2, 200);
            OptionsButton.Button.Text = "Optionen";
            OptionsButton.Button.Parent = mainWindow;
            OptionsButton.Button.Click += new TomShane.Neoforce.Controls.EventHandler(OptionsButton_Click);

            ExitButton = new MenuItemButton();
            ExitButton.Init(manager, content);
            ExitButton.Button.Text = "Spiel beenden";
            ExitButton.Position = new Vector2(graphics.PreferredBackBufferWidth / 2 - ExitButton.Button.Width / 2, 400);
            ExitButton.Button.Parent = mainWindow;
            ExitButton.Button.Click += new TomShane.Neoforce.Controls.EventHandler(ExitButton_Click);

            manager.Add(mainWindow);
        }

        /// <summary>
        /// Setzt alle Controls anhand der Bildschirmaufloesung (graphics.PreferredBackBuffer)
        /// </summary>
        void UpdateCtrls()
        {
            mainWindow.Resizable = true;
            mainWindow.Movable = true;
            mainWindow.MaximumWidth = 5000;
            mainWindow.MaximumHeight = 5000;
            mainWindow.Width = graphics.PreferredBackBufferWidth;
            mainWindow.Height = graphics.PreferredBackBufferHeight;
            mainWindow.Movable = false;
            mainWindow.Resizable = false;
            mainWindow.Center();
            mainWindow.Refresh();

            //Background-PictureBox Size neu setzen
            BackGroundImageBox.MaximumHeight = graphics.PreferredBackBufferHeight;
            BackGroundImageBox.MaximumWidth = graphics.PreferredBackBufferWidth;
            BackGroundImageBox.Height = graphics.PreferredBackBufferHeight;
            BackGroundImageBox.Width = graphics.PreferredBackBufferWidth;
            BackGroundImageBox.BringToFront();

            StartGameButton.Position = new Vector2(graphics.PreferredBackBufferWidth / 2 - StartGameButton.Button.Width / 2, 25);
            StartGameButton.Button.BringToFront();
            OptionsButton.Position = new Vector2(graphics.PreferredBackBufferWidth / 2 - OptionsButton.Button.Width / 2, 200);
            OptionsButton.Button.BringToFront();
            ExitButton.Position = new Vector2(graphics.PreferredBackBufferWidth / 2 - ExitButton.Button.Width / 2, 400);
            ExitButton.Button.BringToFront();
            
        }

        public void UnloadContent()
        {
            manager.Remove(mainWindow);
            if (optionsWindow != null)
            {
                manager.Remove(optionsWindow);
                optionsWindow.Dispose();
            }

            mainWindow.Dispose();


            Properties.Settings.Default.Save();
        }

        public void Update(GameTime gameTime)
        {
            //UpdateCtrls();
            manager.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            manager.Draw(gameTime);
        }
    }
}